package trism.game.controller {
	import trism.field.Field;
	import trism.game.GameScreen;
	import trism.game.model.GameModel;
	import trism.game.model.common.dyn.DynamicItem;

	import com.samoiloff.framework.AbstractComponent;

	import flash.events.Event;

	/**
	 * @author Samoiloff  http://samoiloff.com/
	 */
	public class GameScreenController extends GameBaseController {
		
		private var _dynamicItem : DynamicItem;
		private var _screen : AbstractComponent;
		
		private var _currentScreen	: int;
		
		public function GameScreenController(model : GameModel) {
			super(model);

			_dynamicItem = _model.commonItem.dynamicItem;
			_dynamicItem.addEventListener(DynamicItem.SCREEN_CHANGED, onScreenChanged);
		}

		private function onScreenChanged(event : Event = null) : void {
			_currentScreen = _dynamicItem.screen;
			clearScreen();
			createScreen();
		}
		
		private function createScreen() : void {
			switch (_currentScreen) {
				case GameScreen.FIELD:
					var field:Field = new Field();
					field.init(_model.mainMc.root, _model.commonItem);
					_screen = field;
					break;
				default:
					throw new Error("unsupported screen : " + _dynamicItem.screen);
				break;
			}

			if (_screen) {
				_view.mainMc.addChildAt(_screen, 0);
			}
		}
		
		
		private function clearScreen() : void {
			if (_screen) {
				_view.mainMc.removeChild(_screen);
				_screen.destroy();
				_screen = null;
			}
			
		}
		
	}
}
